package com.ruoyi.base.service.impl;

import java.util.List;

import com.ruoyi.base.ch340.Ch340Configure;
import com.ruoyi.base.ch340.Ch340Service;
import com.ruoyi.base.tool.CHeck16;
import com.ruoyi.base.tool.TypeConverter;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.WaterCardMapper;
import com.ruoyi.base.domain.WaterCard;
import com.ruoyi.base.service.IWaterCardService;

/**
 * 充值Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-24
 */
@Service
public class WaterCardServiceImpl implements IWaterCardService
{
    @Autowired
    private WaterCardMapper waterCardMapper;

    @Autowired
    Ch340Service ch340Service;

    /**
     * 查询充值
     *
     * @param id 充值主键
     * @return 充值
     */
    @Override
    public WaterCard selectWaterCardById(Long id)
    {
        return waterCardMapper.selectWaterCardById(id);
    }

    /**
     * 查询充值列表
     *
     * @param waterCard 充值
     * @return 充值
     */
    @Override
    public List<WaterCard> selectWaterCardList(WaterCard waterCard)
    {
        return waterCardMapper.selectWaterCardList(waterCard);
    }

    /**
     * 新增充值
     *
     * @param waterCard 充值
     * @return 结果
     */
    @Override
    public int insertWaterCard(WaterCard waterCard)
    {


        try {
            String type = waterCard.getCardType();
            String pay = waterCard.getCardPay();
            String send;
            if (type.equals("1")){
                send = Ch340Configure.X14;
            }else {
                send = Ch340Configure.X15;
            }

            String to16v4 = TypeConverter.type10to16v4(pay);

            send = send + to16v4;

            String write = CHeck16.write(send);

            String read = ch340Service.sendAndRead(write);

            CHeck16.read(read);

        } catch (RuntimeException e) {
            throw new RuntimeException(e.getMessage());
        }

        waterCard.setCreateTime(DateUtils.getNowDate());
        return waterCardMapper.insertWaterCard(waterCard);
    }

    /**
     * 修改充值
     *
     * @param waterCard 充值
     * @return 结果
     */
    @Override
    public int updateWaterCard(WaterCard waterCard)
    {
        return waterCardMapper.updateWaterCard(waterCard);
    }

    /**
     * 批量删除充值
     *
     * @param ids 需要删除的充值主键
     * @return 结果
     */
    @Override
    public int deleteWaterCardByIds(Long[] ids)
    {
        return waterCardMapper.deleteWaterCardByIds(ids);
    }

    /**
     * 删除充值信息
     *
     * @param id 充值主键
     * @return 结果
     */
    @Override
    public int deleteWaterCardById(Long id)
    {
        return waterCardMapper.deleteWaterCardById(id);
    }
}
